/*
 * @filename DefaultUserDetailsService.java
 * 
 * @author barry
 * 
 * @version 0.0.1
 * 
 * @date 2020年1月3日
 */
package com.bnzj.cloud.security;

import javax.annotation.Resource;

import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import com.bnzj.cloud.business.core.service.SysUserService;
import org.springframework.stereotype.Component;

import com.bnzj.core.web.security.SecurityResourceManager;
import com.bnzj.core.web.security.SecurityUser;
import com.bnzj.core.web.security.WebUserDetailsService;

/**
 * @author barry
 * @date 2020-01-03
 */
@Component
public class SecurityUserService implements WebUserDetailsService {

    @Resource
    private SecurityResourceManager securityResourceManager;

    @Resource
    private SysUserService userService;

    @Override
    public SecurityUser getByUsername(String username) {
        SysUser sysUser = userService.eagerlyFindByAccount(username);
        if (null == sysUser) {
            return null;
        }
        boolean enabled = sysUser.getEnabled() == null ? false : sysUser.getEnabled();
        boolean expired = sysUser.getPersonnel() == null ? true : sysUser.getPersonnel();
        return new SecurityUser(securityResourceManager, username, sysUser.getCname(), sysUser.getEmail(), sysUser.getPassword(), enabled, expired);
    }
}
